package com.kzn.controller;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kzn.entity.Project;
import com.kzn.service.IProjectService;
import com.kzn.service.impl.ProjectServiceImpl;
import com.kzn.util.FFmpegUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sorclown
 * @since 2022-02-05
 */
@RestController
@RequestMapping("/api/project")
public class ProjectController {
    private IProjectService projectService;

    public ProjectController() {
    }

    @Autowired
    public ProjectController(ProjectServiceImpl projectService) {
        this.projectService = projectService;
    }

    /**
     * 创建项目
     *
     * @param project 创建的项目
     * @return 是否成功将项目放入数据库。
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String insertOne(@RequestBody Project project) {
        if (null != project) {
            if (projectService.insertOne(project, project.getPublisherId())) {
                QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
                projectQueryWrapper.eq("publisherId", project.getPublisherId());
                projectQueryWrapper.eq("projectName", project.getProjectName());
                return JSONUtil.parse(projectService.getOne(projectQueryWrapper)).toString();
            } else {
                return null;
            }
        } else {
            return null;
        }
    }


    /**
     * 上传压缩包
     *
     * @param multipartFile 上传的资源
     * @return 资源是否上传成功
     */
    @RequestMapping(value = "/uploadResource", method = RequestMethod.POST)
    public boolean uploadResource(@RequestParam("file") MultipartFile multipartFile, @RequestParam Integer projectId) {
        if (null != projectId && multipartFile != null && projectService.uploadResource(projectId, multipartFile)) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 审核通过并将项目分割为子任务
     *
     * @param projectId 审核通过的项目ID
     * @param adminId   审核项目的管理员ID
     * @return 拆分项目为子任务是否成功。
     */
    @RequestMapping(value = "/splitToTask", method = RequestMethod.GET)
    public boolean splitToTask(@RequestParam Integer projectId, @RequestParam Integer adminId) {
        if (null != projectId && null != adminId) {
            Project project = projectService.getById(projectId);
            if (null == project.getAdminId()) {
                return projectService.splitToTask(project, adminId);
            }
        }
        return false;
    }

    /**
     * 获取未审核的项目
     *
     * @return 所有状态为未审核的 project 的列表，格式为Json。
     */
    @RequestMapping(value = "/selectUnDoneProject", method = RequestMethod.GET)
    public String selectUnDoneProject() {
        QueryWrapper<Project> wrapper = new QueryWrapper<>();
        wrapper.eq("status", -1);
        List<Project> undoneProject = projectService.list(wrapper);
        return JSONUtil.parseArray(undoneProject).toString();
    }


    /**
     * 返回任务百分比
     *
     * @param projectId 项目ID
     * @return 百分比
     */
    @RequestMapping(value = "/getProjectPercentage", method = RequestMethod.GET)
    public Double getProjectPercentage(@RequestParam Integer projectId) {
        Project project = projectService.getById(projectId);
        Double res = Double.parseDouble(String.format("%.2f", projectService.getProjectPercentage(projectId, project.getParticleSize())));
        System.out.println(res);
        return res;
    }


    @RequestMapping(value = "/setProjectStatusById", method = RequestMethod.GET)
    public Boolean setProjectStatusById(@RequestParam Integer projectId, @RequestParam(required = false) Integer status) {
        if (null != projectId) {
            if (null == status) {
                status = 1;
            } else {
                status += 1;
            }
            UpdateWrapper<Project> projectUpdateWrapper = new UpdateWrapper<>();
            projectUpdateWrapper.eq("id", projectId);
            projectUpdateWrapper.set("status", status);
            projectService.update(projectUpdateWrapper);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 交付项目
     *
     * @param projectId
     * @return
     */
    @RequestMapping(value = "/setProjectOver", method = RequestMethod.GET)
    public Boolean setProjectOver(@RequestParam Integer projectId) {
        if (null != projectId) {
            System.out.println(projectId);
        }
        return null;
    }

    /**
     * 获取 project 列表，可以根据 publisherId 和 status 筛选
     *
     * @param publisherId 任务发布者ID
     * @param status      想要查询 project 的状态
     * @return 查询到的 project 列表，格式为 Json。
     */
    @RequestMapping(value = "/selectProjectByPublisherId", method = RequestMethod.GET)
    public String selectProjectByPublisherId(@RequestParam Integer publisherId, @RequestParam(required = false) Integer status) {
        if (null != publisherId) {
            //先查看是否有子任务都完成，但是未被标记为完成的项目。
            projectService.checkOverProject();
            //查询所有完成的项目
            QueryWrapper<Project> wrapper = new QueryWrapper<>();
            wrapper.eq("publisherId", publisherId);
            if (null != status) {
                //如果没有条件则查询所有的项目
                wrapper.eq("status", status);
            }
            List<Project> list = projectService.list(wrapper);

            //转换为json格式返回
            return JSONUtil.parseArray(list).toString();

        }
        return null;
    }


    /**
     * 获取 project 列表，可以根据 adminID 和 status 筛选
     *
     * @param adminId 管理员ID
     * @param status  想要查询 project 的状态
     * @return 查询到的 project 列表，格式为 Json。
     */
    @RequestMapping(value = "/selectProjectByAdminId", method = RequestMethod.GET)
    public String selectProjectByAdminId(@RequestParam Integer adminId, @RequestParam Integer status) {
        if (null != adminId) {

            //先查看是否有子任务都完成，但是未被标记为完成的项目。
            projectService.checkOverProject();
            //查询所有完成的项目
            QueryWrapper<Project> wrapper = new QueryWrapper<>();
            if (null != status) {
                //如果没有条件，就查询所有项目。
                wrapper.eq("status", status);
            }
            wrapper.eq("adminId", adminId);
            List<Project> list = projectService.list(wrapper);

            //转换为json格式返回
            return JSONUtil.parseArray(list).toString();
        }

        return null;
    }


    /**
     * 标记项目
     *
     * @param project 想要废弃的项目
     * @return 返回更新是否成功。
     */
    @RequestMapping(value = "/trashProject", method = RequestMethod.POST)
    public boolean trashProject(@RequestBody Project project) {

        project.setStatus(2);
        return projectService.updateById(project);

    }


    /**
     * 获取结果
     *
     * @param projectId
     * @param type
     * @param res
     * @return
     */
    @RequestMapping(value = "/getResultByProjectId", method = RequestMethod.GET)
    public String getResultByProjectId(@RequestParam Integer projectId, @RequestParam(required = false) String type, HttpServletResponse res) {
        if (null != projectId) {
            return projectService.getResultByProjectId(projectId, type, res);
        }
        return null;
    }


    @RequestMapping(value = "/getResourceById",method = RequestMethod.GET)
    public String getResourceById(@RequestParam Integer projectId, HttpServletResponse res){
        if (null!=projectId){
            return projectService.getResourceById(projectId,res);
        }
        return null;
    }
}




